package lang

import (
	"jvm/native"
	"jvm/rtda"
	"jvm/rtda/heap"
	"strings"
)

func init() {
	native.Register("java/lang/Class", "getPrimitiveClass",
		"(Ljava/lang/String;)Ljava/lang/Class;", getPrimitiveClass)
	native.Register("java/lang/Class", "getName0", "()Ljava/lang/String;", getName0)
	native.Register("java/lang/Class", "desiredAssertionStatus0",
		"(Ljava/lang/Class;)Z", desiredAssertionStatus0)
}

// static native Class<?> getPrimitiveClass(String name);
func getPrimitiveClass(frame *rtda.Frame) {
	stringObj := frame.LocalVars().GetRef(0)
	name := heap.GoString(stringObj)
	class := stringObj.Class().Loader().LoadClass(name)
	frame.OPStack().PushRef(class.JClass())
}

// private native String getName0();
func getName0(frame *rtda.Frame) {
	this := frame.LocalVars().GetRef(0)
	class := this.Extra().(*heap.Class)
	goStringName := strings.ReplaceAll(class.Name(), "/", ".")
	jString := heap.JString(class.Loader(), goStringName)
	frame.OPStack().PushRef(jString)
}

// private static native boolean desiredAssertionStatus0(Class<?> clazz);
func desiredAssertionStatus0(frame *rtda.Frame) {
	frame.OPStack().PushBoolean(false)
}
